package com.google.code.rgbgoesbetween.gui;

import java.util.Arrays;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.google.code.rgbgoesbetween.GlobalConstants;
import com.google.code.rgbgoesbetween.R;
import com.google.code.rgbgoesbetween.data.Item.ItemStatus;
import com.google.code.rgbgoesbetween.data.SearchEngineCriteria;
import com.google.code.rgbgoesbetween.services.DataProvider;
/**
 * When opened, creates an activity that allows the user to search the database for Items
 * using the search criteria provided by the SearchEngineCriteria class.
 * @author Avanyali
 *
 */
public class AdvancedSearchActivity extends Activity implements OnClickListener {

	private Spinner statusSelector;
	private Button submitButton;
	private EditText categorySelector;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_adv_search);
		
		statusSelector = (Spinner) findViewById(R.id.item_status_spinner);
		submitButton = (Button) findViewById(R.id.submit_button);
		submitButton.setOnClickListener(this);
		categorySelector = (EditText) findViewById(R.id.search_description_input);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_home, menu);
		return true;
	}
	
	/**
	 * When the search button is clicked, starts the ItemListDisplayActivity
	 */
	@Override
	public void onClick(View v) {
		if(v == submitButton) {
			Intent showItemsIntent = new Intent(this, ItemListDisplayActivity.class);
			SearchEngineCriteria criteria = new SearchEngineCriteria(null, null, categorySelector.getText().toString(),
					ItemStatus.values()[statusSelector.getSelectedItemPosition()] == ItemStatus.LOST, 0.0);
			int[] matchingIds = DataProvider.getDataProvider().findMatchingIds(criteria);
			Log.e("Found", Arrays.toString(matchingIds));
			showItemsIntent.putExtra(GlobalConstants.ITEM_ID, matchingIds);
			startActivity(showItemsIntent);
		}
	}

}
